package com.ziytek.web.citizen.configuration;

import com.ziytek.web.citizen.config.caffeine.CaffeineUtil;
import com.ziytek.web.citizen.constant.NumConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;

/**
 * 数据库新增数据拦截器
 *
 * @author fenghx
 */
@Intercepts({@Signature(type = Executor.class,
        method = "update",
        args = {MappedStatement.class, Object.class})
})
@Slf4j
public class InsertInterceptor implements Interceptor {
    @Override
    public Object intercept(org.apache.ibatis.plugin.Invocation invocation) throws Throwable {
        var ms = (MappedStatement) invocation.getArgs()[NumConstant.INT_ZERO];
        if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
            //Object parameter = invocation.getArgs()[NumConstant.INT_ONE];
            //log.info("检测到新增操作，参数：{}", parameter);

            // 新增数据拦截器，设置数据新增标识缓存
            CaffeineUtil.set(CaffeineUtil.INTERCEPTOR_INSERT, Boolean.TRUE, NumConstant.INT_TWO_TEN);
        }
        return invocation.proceed();
    }
}
